#!/usr/bin/python
# -*- coding: UTF-8 -*-

from abc import ABCMeta, abstractmethod

import ConfigParser

from proxyips.mysqlmoudel.sqlmoudle import sqlmoudle


class Builder:
    """
    @abstractmethod 为抽象方法
    """
    __metaclass__ = ABCMeta

    @abstractmethod
    def build_server_address(self):
        """
        mysql的server 地址
        :return: 
        """
        pass

    @abstractmethod
    def build_username(self):
        pass

    @abstractmethod
    def build_password(self):
        pass

    @abstractmethod
    def build_port(self):
        pass

    @abstractmethod
    def use_db(self):
        pass


class MysqlBuilder(Builder):
    def __init__(self,configfile,configname):
        self.sqlmoudle = sqlmoudle()  # 链接mysql需要的必要信息
        self.configfile = configfile  # 配置文件
        self.configname = configname  # 配置项
        self.config = ConfigParser.ConfigParser()
        self.config.readfp(open(configfile,'rb')) # 读取配置文件

    """
    下面的函数是将配置文件写入sqlmoudle里
    """
    def build_port(self):
        self.sqlmoudle.port = int(self.config.get(self.configname, 'port'))

    def build_server_address(self):
        self.sqlmoudle.host = self.config.get(self.configname, 'host')

    def build_password(self):
        self.sqlmoudle.passwd = self.config.get(self.configname, 'passwd')

    def use_db(self):
        self.sqlmoudle.db = self.config.get(self.configname, 'db')

    def build_username(self):
        self.sqlmoudle.user = self.config.get(self.configname, 'user')

    def build_charset(self):
        self.sqlmoudle.charset = self.config.get(self.configname, 'charset')
    
    def build_useunicode(self):
        self.sqlmoudle.use_unicode = self.config.get(self.configname, 'use_unicode')


    def get_moudle(self):
        """
        返回moudel
        :return: 
        """
        return self.sqlmoudle


    def build_all(self):
        """
        同时设置所有
        :return: 
        """
        self.build_password()
        self.build_port()
        self.build_server_address()
        self.build_password()
        self.build_username()
        self.use_db()
        self.build_charset()
        self.build_useunicode()
        return self

    def get_tuples(self):
        """
        返回必要的信息
        :return: 
        """
        return self.sqlmoudle.host, self.sqlmoudle.user, self.sqlmoudle.passwd, self.sqlmoudle.port
